package com.goods.category.controller;

import com.goods.category.service.CategoryService;
import com.goods.entity.Category;
import com.goods.povos.CategoryPovo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.MalformedObjectNameException;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/Category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
//获取所有分类树
    @RequestMapping("/GetCategory")
    public String getcategory(HttpSession session){
        List<CategoryPovo> categoryPovos = categoryService.loadCategories();
        session.setAttribute("categorypovos", categoryPovos);
        return "adminjsps/admin/category/list";
    }

    @RequestMapping("/GetCategoryById")
    @ResponseBody
    public Category getCategoryById(String cid){
        return categoryService.getCategoryById(cid);
    }

//添加一级类并返回列表
    @RequestMapping("/AddOneCategory")
    public String addOneCategory(Category category){
        System.out.println(category.toString());
        categoryService.addOneCategory(category);
        return "redirect:/Category/GetCategory";
    }
    //添加二级类并返回列表
    @RequestMapping("/AddTwoCategory")
    public String addTwoCategory(Category category){
        System.out.println(category.toString());
        categoryService.addTwoCategory(category);
        return "redirect:/Category/GetCategory";
    }
//    删除分类
    @RequestMapping("/DeleteCategory")
    public String deleteCategory(String cid){
        System.out.println(cid);
        categoryService.deleteCategory(cid);
        return "redirect:/Category/GetCategory";
    }

    //    修改回显
    @RequestMapping("/edit")
    public String edit(String cid, Model model){
        model.addAttribute("cid",cid);
        return "adminjsps/admin/category/edit";
    }
    //    修改回显
    @RequestMapping("/edit2")
    public String edit2(String cid, Model model){
        model.addAttribute("cid",cid);
        return "adminjsps/admin/category/edit2";
    }
//    修改分类
    @RequestMapping("/UpdateCategory")
    public String updateCategory(Category category){
        System.out.println(category);
        categoryService.updateCategory(category);
        return "redirect:/Category/GetCategory";
    }



//    获取所有列表
//    add 一级列表
//    add 二级列表
//    update 一级列表
//    update 二级列表
//    delete 一级
//    delete 二级

//    删除分类时将门下的图书也删除？
}
